﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using suanpan.API.Components.Model;
using System.Configuration;
using System.Xml.Serialization;

namespace suanpan.API.Components.Config
{
    public class ApiConsumerConfig
    {
        public ApiConsumerConfig() { }

        private static ApiConsumerConfig _instance = (ApiConsumerConfig)ConfigurationManager.GetSection("ApiConsumerConfig");

        public static ApiConsumerConfig GetInstance()
        {
            if ( _instance == null )
                throw new ConfigurationErrorsException("Failed to locate or deserialize the 'ApiConsumerConfiguration' section.");

            return _instance;
        }

        private ApiConsumer[] _apiConsumers;
        public ApiConsumer[] ApiConsumers
        {
            get { return _apiConsumers; }
            set { _apiConsumers = value; }
        }

        public ApiConsumer this[string consumerKey]
        {
            get
            {
                for ( int i = 0; i < _apiConsumers.Length; i++ )
                {
                    if ( _apiConsumers[i].ConsumerKey == consumerKey )
                        return _apiConsumers[i];
                }
                return null;
            }
        }
    }
}
